/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package physical_spaces.beans;

import Entity.ControlFile;
import java.sql.Connection;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import login.beans.ConectionBean;
import physical_spaces.business.ControlFileDataModel;
import physical_spaces.business.CreateControlFileBusiness;
import physical_spaces.business.ListControlFileBusiness;

/**
 *
 * @author jhtobav
 */
@ManagedBean
@RequestScoped
public class ControlFileBean {
    
    private String name;
    private String address;
    private List<ControlFile> controlFiles;
    private ControlFileDataModel mediumControlFileModel;
    private static final long serialVersionUID = 308L;
    private ControlFile selectedControlFile;   
    private ListControlFileBusiness listControlFileBusiness;
    private Connection conn;

    public ControlFileBean() {
        
        address = "";
        chargeData();
        
        if(controlFiles.get(0) != null){
            address = controlFiles.get(0).getName();
            int lastIndex = address.lastIndexOf("\\");
            address = address.substring(0, lastIndex+1);
        }else{
            address = "";
        }
        
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    
    public List<ControlFile> getControlFiles() {
        return controlFiles;
    }

    public void setControlFiles(List<ControlFile> controlFiles) {
        this.controlFiles = controlFiles;
    }

    public ControlFileDataModel getMediumControlFileModel() {
        return mediumControlFileModel;
    }

    public void setMediumControlFileModel(ControlFileDataModel mediumControlFileModel) {
        this.mediumControlFileModel = mediumControlFileModel;
    }

    public ControlFile getSelectedControlFile() {
        return selectedControlFile;
    }

    public void setSelectedControlFile(ControlFile selectedControlFile) {
        this.selectedControlFile = selectedControlFile;
    }

    public ListControlFileBusiness getListControlFileBusiness() {
        return listControlFileBusiness;
    }

    public void setListControlFileBusiness(ListControlFileBusiness listControlFileBusiness) {
        this.listControlFileBusiness = listControlFileBusiness;
    }

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }
    
    public void chargeData(){
        
        FacesContext context = javax.faces.context.FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        ConectionBean conectionBean =(ConectionBean) session.getAttribute("conectionBean");        
        this.conn = conectionBean.currentConnection;
        listControlFileBusiness = new ListControlFileBusiness(conn);
        controlFiles = listControlFileBusiness.listControlFiles();
        mediumControlFileModel = new ControlFileDataModel(controlFiles);
        System.out.println(".:::::::::::::::::::::::....::::::::::");
        
    }
    public String createControlFile( Connection conn ) {

        CreateControlFileBusiness createControlFileBusiness = new CreateControlFileBusiness();
        if("logout".equals(createControlFileBusiness.createControlFile(controlFiles,conn,name,address))){
            return "/login";
        }
        return "PhysicalSpaces/ControlFile.xhtml";
        
    }

}
